﻿using QFramework;
using System.Collections.Generic;
using UnityEngine;

namespace ProjectIndieFarm
{
    public class ToolbarSystem : AbstractSystem, IToolbarSystem
    {
        public static EasyEvent<ToolbarSlot, int> OnItemCountChange { get; set; } = new();

        protected override void OnInit()
        {

        }

        public List<ToolbarSlot> Slots { get; } = new() {
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
        };

        private readonly List<ToolbarSlot> mInitSlotsConfig = new()
        {
            new ToolbarSlot("hand",1),
            new ToolbarSlot("hoe",1),
            new ToolbarSlot("water_pot",1),
            new ToolbarSlot("seed_potato",16),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
        };

        #region IArchive
        /// <summary>
        /// 保存道具栏数据
        /// </summary>
        public void SaveData()
        {
            for (int i = 0; i < Slots.Count; i++)
            {
                PlayerPrefs.SetString($"toolbar_slot_{i}_item_id", Slots[i].ItemID.Value);
                PlayerPrefs.SetInt($"toolbar_slot_{i}_count", Slots[i].Count.Value);
            }
        }
        /// <summary>
        /// 加载道具栏数据
        /// </summary>
        public void LoadData()
        {
            for (int i = 0; i < Slots.Count; i++)
            {
                Slots[i].ItemID.Value = PlayerPrefs.GetString($"toolbar_slot_{i}_item_id", mInitSlotsConfig[i].ItemID.Value);
                Slots[i].Count.Value = PlayerPrefs.GetInt($"toolbar_slot_{i}_count", mInitSlotsConfig[i].Count.Value);
            }
        }
        /// <summary>
        /// 重置道具栏数据
        /// </summary>
        public void ResetData()
        {
            for (int i = 0; i < Slots.Count; i++)
            {
                var toolbarSlot = Slots[i];
                toolbarSlot.ItemID.Value = mInitSlotsConfig[i].ItemID.Value;
                toolbarSlot.Count.Value = mInitSlotsConfig[i].Count.Value;
            }
            SaveData();
        }
        #endregion
    }
}
